package thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Classname PrintLock
 * @Description TODO
 * @Date 2024/1/6 17:07
 * @Created by ZHL17
 */

/**
 * 多线程顺序打印ABC10次
 */
class PrintLock {
    private static int count = 10;
    private static int number = 0;

    private static ReentrantLock lock = new ReentrantLock();

    public static void printMessage(int mod, String message) {
        for (int i = 0; i < count;) {
            lock.lock();
            if (number % 3 == mod) {
                number++;
                System.out.print(message);
                i++;
            }
            lock.unlock();
        }
    }
}
public class MultiThread {
    public static void main(String[] args) {
        new Thread(() -> {
            PrintLock.printMessage(0, "A");
        }).start();

        new Thread(() -> {
            PrintLock.printMessage(1, "B");
        }).start();

        new Thread(() -> {
            PrintLock.printMessage(2,"C");
        }).start();
    }

}

